package org.example.day20240815;

import java.util.Arrays;

public class MyArrays {
    public static void fill(int[]x,int y){
        for (int i = 0, len = x.length; i < len; i++)
            x[i] = y;
    }

    public static String toString(int[] arr){
        String result = "[";
        for(int i=0; i<arr.length; i++){
            if(i !=arr.length-1){
                result+=arr[i]+", ";
            }
        }
        return result+"]";
    }

    public static int find(int[] arr, int x){
        int result = 0;
        for(int i = 0; i<arr.length; i++){
            if(x == arr[i])
                result = i;
            else result = -1;
        }
        return result;
    }

    public static int findMax(int[] arr){
        int result = 0;
        for(int i=0; i< arr.length; i++){
            if(arr[i]>arr[i+1]){
                result = arr[i];
            }
        }
        return result;
    }

    public static void sort(int[] arr){
        int x = 0;
        for(int i=0; i<arr.length; i++){
            for(int j=i+1; j<arr.length; j++){
                if(arr[j]<arr[i]){
                    x=arr[i];
                    arr[i] = arr[j];
                    arr[j] = x;
                }
            }
        }
    }

    public static int findZheban(int[] arr, int value){
        int left = 0;
        int right = arr.length - 1;
        while (left <= right){
            int mid = (left + right)/2;
            if(arr[mid] == value){
                return mid;
            }
            else if(arr[mid] < value){
                left = mid + 1;
            }
            else{
                right = mid - 1;
            }
        }

        return -1;
    }
}
